package com.lms.stream;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-08 08:05
 * @Description:
 */
public class StreamSupportDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ArrayList<Object> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        Stream<Object> stream = StreamSupport.stream(list.spliterator(), true);
        System.out.println("----------->主线程：" + Thread.currentThread().getName());
//        stream.forEach(n -> {
//            try {
//                Thread.sleep(2000);
//                System.out.println(n + "  ------>子线程：" + Thread.currentThread().getName());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
//        System.out.println("------------");

        //守护线程池
        ForkJoinPool forkJoinPool = new ForkJoinPool(2);
        forkJoinPool.submit(() -> {
            stream.forEach(n -> {
                        try {
                            Thread.sleep(2000);
                            System.out.println(n + "  ------>子线程：" + Thread.currentThread().getName());
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
            );
        }).get();


    }
}
